import os;
import time;
import numpy;

# 用公式π/4=1-1/3+1/5-1/7...求π的近似值
def  pi_calculate_pc():
	S = 4000
	# sign = 1
	pi = 1.0
	n = 1.0
	term = 1.0
	tm_start  = time.time()

	for i in range(int(S)) :
		# print(i % 2)
		n = n + 2
		if i % 2:
			pi = pi + 1/n;
			# print(str(i)+"+1/"+ str(n))
		else:
			pi = pi - 1/n;
			# print(str(i)+"-1/"+ str(n))

	tm_end  = time.time()
	print("calculate times ="+str(S)+",used time="+str(tm_end - tm_start));
	print("Pi=",pi*4)
def  pi_calculate_micro_64():
	S = 4000
	# sign = 1

	pi = numpy.float64(1.0);
	n = numpy.float64(1.0);
	term = numpy.float64(1.0);
	tm_start  = time.time()

	for i in range(int(S)) :
		# print(i % 2)
		n = n + 2
		if i % 2:
			pi = pi + 1/n;
			# print(str(i)+"+1/"+ str(n))
		else:
			pi = pi - 1/n;
			# print(str(i)+"-1/"+ str(n))

	tm_end  = time.time()
	print("calculate times ="+str(S)+",used time="+str(tm_end - tm_start));
	print("Pi=",pi*4)
def  pi_calculate_micropython():
	S = 2000
	# sign = 1
	pi = 10000000
	fen_mu_1 = pi
	n = 1

	tm_start  = time.time()

	for i in range(int(S)) :
		# print(i % 2)
		n = n + 2
		if i % 2:
			pi = pi + fen_mu_1/n;
			# print(str(i)+"+1/"+ str(n))
		else:
			pi = pi - fen_mu_1/n;
			# print(str(i)+"-1/"+ str(n))

	tm_end  = time.time()
	print("calculate times ="+str(S)+",used time="+str(tm_end - tm_start));
	print("Pi=",pi*4)

if __name__ == '__main__':
	# pi_calculate_micropython();
	# pi_calculate_pc()
	pi_calculate_micro_64();

